{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying Lattice Execution Time\n",
    "\n",
    "Calculate execution time of a workflow (lattice) after it ends by querying the `Result` object and subtracting the start time from the end time.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Define a [workflow](../execution/execute_lattice.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "import time\n",
    "\n",
    "@ct.electron\n",
    "def add(x, y):\n",
    "    time.sleep(2)\n",
    "    return x + y\n",
    "\n",
    "@ct.electron\n",
    "def multiply(x, y):\n",
    "    return x * y\n",
    "\n",
    "@ct.lattice\n",
    "def workflow(x, y):\n",
    "    res_1 = add(x=x, y=y)\n",
    "    return multiply(x=res_1, y=y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. Use the Covalent `dispatch()` function to dispatch the workflow."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dispatch_id = ct.dispatch(workflow)(x=2, y=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Retrieve the Covalent result object, setting the `wait` parameter to `True` so that execution finishes before the result is retrieved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = ct.get_result(dispatch_id=dispatch_id, wait=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Use the result object's `start_time` and `end_time` attributes to calculate the duration of the dispatch:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Execution time: 2.18 seconds\n"
     ]
    }
   ],
   "source": [
    "print(f\"Execution time: {(result.end_time - result.start_time).total_seconds():.2f} seconds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Dispatching a Workflow](../execution/execute_lattice.ipynb)\n",
    "\n",
    "[Querying the Status of a Lattice](./query_lattice_execution_status.ipynb)\n",
    "\n",
    "[Querying the Status of an Electron](./query_electron_execution_status.ipynb)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f63e7985caea997504032bc67ebf6b6ff07db5420026db9c4de4a9a029ce747c"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
